﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace TableEmanee.Dal
{
    class TableDataEntity
    {
      public Int32 ID {get;set;}
      public Int32 Mem_Id { get; set; }
      public Int32 praying_in_groups { get; set; }
      public Int32 Nawafel { get; set; }
      public Int32 Qiyam { get; set; }
      public Int32 Reading_Quran { get; set; }
      public Int32 Remember_Quran { get; set; }
      public Int32 Morning_And_Evening { get; set; }
      public Int32 Other { get; set; }
      public Int32 Record { get; set; }
      public Int32 Liqaa_Magmoaa { get; set; }
      public Int32 khotbt_Gomaa { get; set; }
      public Int32 Dars_Gomaa { get; set; }
      public Int32 LIqaa_Thaqafi { get; set; }
      public Int32 Liqaa_Aam { get; set; }
    }

    class TableData
    {
        private static String connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='evaluation.accdb' ;Jet OLEDB:Database Password=Yarabe11";
        private static String cmdText = "";


        public static bool insert(TableDataEntity m)
        {
            bool flag = false;
            cmdText = "INSERT INTO TableData (Mem_Id,praying_in_groups,Nawafel,Qiyam,Reading_Quran,Remember_Quran,Morning_And_Evening,Other,Record,Liqaa_Magmoaa,khotbt_Gomaa,Dars_Gomaa,LIqaa_Thaqafi,Liqaa_Aam) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@Mem_Id", m.Mem_Id);
            cmd.Parameters.AddWithValue("@praying_in_groups", m.praying_in_groups);
            cmd.Parameters.AddWithValue("@Nawafel", m.Nawafel);
            cmd.Parameters.AddWithValue("@Qiyam", m.Qiyam);
            cmd.Parameters.AddWithValue("@Reading_Quran", m.Reading_Quran);
            cmd.Parameters.AddWithValue("@Remember_Quran", m.Remember_Quran);
            cmd.Parameters.AddWithValue("@Morning_And_Evening", m.Morning_And_Evening);
            cmd.Parameters.AddWithValue("@Other", m.Other);
            cmd.Parameters.AddWithValue("@Record", m.Record);
            cmd.Parameters.AddWithValue("@Liqaa_Magmoaa", m.Liqaa_Magmoaa);
            cmd.Parameters.AddWithValue("@khotbt_Gomaa", m.khotbt_Gomaa);
            cmd.Parameters.AddWithValue("@Dars_Gomaa", m.Dars_Gomaa);
            cmd.Parameters.AddWithValue("@LIqaa_Thaqafi", m.LIqaa_Thaqafi);
            cmd.Parameters.AddWithValue("@Liqaa_Aam", m.Liqaa_Aam);
         


            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Insert" + e.Message);
            }
            finally
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;

        }
        public static bool Update(TableDataEntity m)
        {
            bool flag = false;
            cmdText = "Update TableData set Mem_Id= @Mem_Id , praying_in_groups = @praying_in_groups ,Nawafel=@Nawafel, Qiyam =@Qiyam Reading_Quran = @Reading_Quran Remember_Quran= @Remember_Quran Morning_And_Evening= @Morning_And_Evening Other= @Other Record= @Record Liqaa_Magmoaa= @Liqaa_Magmoaa  khotbt_Gomaa= @khotbt_Gomaa  Dars_Gomaa= @Dars_Gomaa  LIqaa_Thaqafi= @LIqaa_Thaqafi  Liqaa_Aam=@Liqaa_Aam  where ID=@ID";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Mem_Id", m.Mem_Id);
            cmd.Parameters.AddWithValue("@praying_in_groups", m.praying_in_groups);
            cmd.Parameters.AddWithValue("@Nawafel", m.Nawafel);
            cmd.Parameters.AddWithValue("@Qiyam", m.Qiyam);
            cmd.Parameters.AddWithValue("@Reading_Quran", m.Reading_Quran);
            cmd.Parameters.AddWithValue("@Remember_Quran", m.Remember_Quran);
            cmd.Parameters.AddWithValue("@Morning_And_Evening", m.Morning_And_Evening);
            cmd.Parameters.AddWithValue("@Other", m.Other);
            cmd.Parameters.AddWithValue("@Record", m.Record);
            cmd.Parameters.AddWithValue("@Liqaa_Magmoaa", m.Liqaa_Magmoaa);
            cmd.Parameters.AddWithValue("@khotbt_Gomaa", m.khotbt_Gomaa);
            cmd.Parameters.AddWithValue("@Dars_Gomaa", m.Dars_Gomaa);
            cmd.Parameters.AddWithValue("@LIqaa_Thaqafi", m.LIqaa_Thaqafi);
            cmd.Parameters.AddWithValue("@Liqaa_Aam", m.Liqaa_Aam);
            cmd.Parameters.AddWithValue("@ID", m.ID);

            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Update" + e.Message);
            }
            finally
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;


        }
        public static bool delete(int id)
        {
            bool flag = true;
            cmdText = "delete from TableData where ID = ? ";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@ID", id);

            try
            {
                con.Open();
                int x = cmd.ExecuteNonQuery();
                if (x == 0)
                {
                    flag = false;

                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in Delet" + e.Message);

            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

            }
            return flag;

        }
        public static TableDataEntity GetById(int x)
        {
            TableDataEntity t = null;
            cmdText = "Select * from TableData where ID=?";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@ID", x);
            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetTableData ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return t;
        }
        public static List<TableDataEntity> GetAll()
        {
            List<TableDataEntity> listEntity = new List<TableDataEntity>();
            TableDataEntity t = null;
            cmdText = "Select * from TableData";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    listEntity.Add(t);

                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetTableData ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return listEntity;

        }
        public static TableDataEntity fillEntityFromReader(OleDbDataReader reader)
        {
            TableDataEntity t = new TableDataEntity();

            if (reader["ID"] != null)
                t.ID = Int32.Parse(reader["ID"].ToString());
            if (reader["Mem_Id"] != null)
                t.Mem_Id = Int32.Parse(reader["Mem_Id"].ToString());
            if (reader["praying_in_groups"] != null)
                t.praying_in_groups = Int32.Parse(reader["praying_in_groups"].ToString());
            if (reader["Nawafel"] != null)
                t.Nawafel = Int32.Parse(reader["Nawafel"].ToString());
            if (reader["Qiyam"] != null)
                t.Qiyam = Int32.Parse(reader["Qiyam"].ToString());
            if (reader["Reading_Quran"] != null)
                t.Reading_Quran = Int32.Parse(reader["Reading_Quran"].ToString());
            if (reader["Remember_Quran"] != null)
                t.Remember_Quran = Int32.Parse(reader["Remember_Quran"].ToString());
            if (reader["Morning_And_Evening"] != null)
                t.Morning_And_Evening = Int32.Parse(reader["Morning_And_Evening"].ToString());
            if (reader["Other"] != null)
                t.Other = Int32.Parse(reader["Other"].ToString());
            if (reader["Record"] != null)
                t.Record = Int32.Parse(reader["Record"].ToString());
            if (reader["Liqaa_Magmoaa"] != null)
                t.Liqaa_Magmoaa = Int32.Parse(reader["Liqaa_Magmoaa"].ToString());
            if (reader["khotbt_Gomaa"] != null)
                t.khotbt_Gomaa = Int32.Parse(reader["khotbt_Gomaa"].ToString());
            if (reader["Dars_Gomaa"] != null)
                t.Dars_Gomaa = Int32.Parse(reader["Dars_Gomaa"].ToString());
            if (reader["LIqaa_Thaqafi"] != null)
                t.LIqaa_Thaqafi = Int32.Parse(reader["LIqaa_Thaqafi"].ToString());
            if (reader["Liqaa_Aam"] != null)
                t.ID = Int32.Parse(reader["Liqaa_Aam"].ToString());

            return t;

        }
 

    }
}
